package com.extra_archive.project.iisu.view.list 
{
	import com.extra_archive.framework.core.mvc.controller.IController;
	import com.extra_archive.framework.core.mvc.model.IModel;
	import com.extra_archive.framework.core.mvc.view.ComponentView;
	import com.extra_archive.framework.core.mvc.view.IComponentView;
	import com.extra_archive.project.iisu.model.ARModel;
	import com.greensock.TweenMax;

	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Plane;

	import mx.effects.easing.Back;

	import flash.events.Event;

	/**
	 * @class name : ARListView
	 * @author : implict (http://extra-archive.com)
	 * @date : 2010. 4. 28.
	 * @description :
	 */
	public class ARList3DView extends DisplayObject3D implements IComponentView 
	{
		private var _model : IModel;
		private var _initialized : Boolean;
		private var _planes : Vector.<Plane> = new Vector.<Plane>();
		
		public function ARList3DView(model : IModel, controller : IController = null)
		{
			_model = model;
			
			initialize();
		}
		
		//--------------------------------------------------------------------------
		//
		//    Public Methods
		//
		//--------------------------------------------------------------------------
		
		public function initialize() : void
		{
			trace(this + " : initialize");
			
			createList();
		}
		
		public function addComponent(component : IComponentView) : void
		{
		}
		
		public function removeComponent(component : IComponentView) : void
		{
		}
		
		public function getChild(index : int) : ComponentView
		{
			// TODO: Auto-generated method stub
			return null;
		}
		
		public function update(event : Event = null) : void
		{
			if (!_initialized) return;
			
			/**
			 * 리스트 이동
			 */
			this.y += (_model.applicationContainer.mouseY - _model.applicationContainer.stage.stageHeight / 2)  / _model.applicationContainer.stage.stageHeight / 2 * 40;
			
			/**
			 * plane 이동
			 */
			if (_planes[0].sceneY < -400)
			{
				_planes[0].y = _planes[_planes.length - 1].y + 110;
				_planes.push(_planes.shift());
				_planes.length = 7;
			}
			
			if (_planes[_planes.length - 1].sceneY > 400)
			{
				_planes[_planes.length - 1].y = _planes[0].y - 110;
				_planes.unshift(_planes.pop());
				_planes.length = 7;
			}
		}
		
		override public function toString() : String
		{
			return "com.extra_archive.project.iisu.view.list.ARListView";
		}
		
		//--------------------------------------------------------------------------
		//
		//    Event Listeners
		//
		//--------------------------------------------------------------------------
		
		private function onIntroComplete(count : uint) : void
		{
			if (count == ARModel(_model).assetManager.listImgs.length - 1) _initialized = true;
		}
		
		//--------------------------------------------------------------------------
		//
		//    Private Methods
		//
		//--------------------------------------------------------------------------
		
		private function createList() : void
		{
			var plane : Plane;
			var material : BitmapMaterial;
			var i : uint, len : uint = ARModel(_model).assetManager.listImgs.length;
			
			for (i; i < len; ++i)
			{
				material = new BitmapMaterial(ARModel(_model).assetManager.listImgs[i].bitmapData, false);
				material.smooth = true;
				plane = new Plane(material, 133, 100);
				plane.name = "plane" + i;
				plane.y = 450;
				plane.alpha = 0;
				addChild(plane);
				_planes.push(plane);
				
				TweenMax.to(plane, 0.5, {delay : i * 0.1 + 0.3, alpha : 1, y : -_model.applicationContainer.stage.stageHeight / 2 + 110 * i + 50, ease: Back.easeOut, onComplete: onIntroComplete, onCompleteParams : [i]});
			}

		}
	}
}
